package top.novel.persistence;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author xuhainan
 * @date 2022/8/8 17:19
 * @region hefei
 */
@State(name = "NovelSetting", storages = @Storage("novel-plugin.xml"))
public class PersistenceService implements PersistentStateComponent<PersistenceData> {

    private PersistenceData persistenceData;

    public static PersistenceService getInstance() {
        return ApplicationManager.getApplication().getComponent(PersistenceService.class);
    }

    @Override
    public @Nullable PersistenceData getState() {
        return getPersistenceData();
    }

    @Override
    public void loadState(@NotNull PersistenceData state) {
        this.persistenceData = state;
    }

    public PersistenceData getPersistenceData() {
        if (persistenceData == null) {
            persistenceData = new PersistenceData();
        }
        return persistenceData;
    }
}
